package com.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.mapper.UserMapper;
import com.demo.model.User;
import com.demo.vo.UserParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sdyang
 * @create 2019-05-01 14:31
 **/
@Service
public class UserService {

    private static Logger logger = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;

    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    public void save(User user) {
        if (user.getId() == null || user.getId() == 0L) {
            user.insert();
        } else {
            user.updateById();
        }
    }

    public void delete(Long id) {
        userMapper.deleteById(id);
    }

    public List<User> find(UserParam param) {
        QueryWrapper<User> ew = new QueryWrapper<User>();
        if (StringUtils.isNotEmpty(param.getUsername())) {
            ew.like("username", param.getUsername());
        }
        if (StringUtils.isNotEmpty(param.getLoginid())) {
            ew.like("loginid", param.getLoginid());
        }
        if (StringUtils.isNotEmpty(param.getMobile())) {
            ew.like("mobile", param.getMobile());
        }
        return userMapper.selectList(ew);
    }

}
